//package pipeline;
//
////import org.neo4j.driver.internal.spi.Connection;
//import model.*;
//import org.neo4j.driver.v1.*;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import us.codecraft.webmagic.ResultItems;
//import us.codecraft.webmagic.Task;
//import us.codecraft.webmagic.pipeline.Pipeline;
//import org.neo4j.driver.v1.exceptions.*;
//import java.sql.*;
//import java.util.ArrayList;
//import java.util.List;
//
//
//public class Neo4jPipeline implements Pipeline {
//    private Logger logger = LoggerFactory.getLogger(getClass());
//
//    private Connection con;
//    public Neo4jPipeline() throws ClassNotFoundException, SQLException{
//        this.con= DriverManager.getConnection("jdbc:neo4j:bolt://localhost","neo4j", "123456");
//
//    }
//
//    @Override
//    public void process(ResultItems resultItems, Task task) {
//        String job_id = resultItems.getRequest().getExtra("job_id").toString();
//
//        BaseRegisterInfo baseRegisterInfo = (BaseRegisterInfo)resultItems.get("BaseRegisterInfo");
//        if(baseRegisterInfo != null && baseRegisterInfo.getCompany_full_name() != null){
//            insertBaseRegisterInfo(baseRegisterInfo,job_id);
//        }
//        BaseBusinessInfo businessInfo = (BaseBusinessInfo)resultItems.get("BaseBusinessInfo");
//        if(businessInfo != null && businessInfo.getCompany_name() !=null){
//            insertBaseBusinessInfo(businessInfo,job_id);
//        }
//
//        ArrayList<BaseShareholderInfo> baseShareholderInfos = resultItems.get("BaseShareholderInfo");
//        if(baseShareholderInfos != null){
//            insertBaseShareholderInfo(baseShareholderInfos,job_id);
//
//        }
//
//
//        ArrayList<BaseChange> baseChanges = resultItems.get("BaseChange");
//        if(baseChanges != null){
//            insertBaseChange(baseChanges,job_id);
//
//        }
//
//
//        ArrayList<EquityPledge> equityPledges = resultItems.get("EquityPledge");
//        if(equityPledges != null){
//            insertEquityPledge(equityPledges,job_id);
//
//        }
//
//
//    }
//    public void run(String query){
//        try (PreparedStatement stmt = con.prepareStatement(query)) {
//            stmt.executeQuery();
//            stmt.executeUpdate();//执行sql语句
//            stmt.close();
//
//        }catch (ClientException e1){
//            e1.getMessage();
//        }
//        catch (SQLException e) {
//            if (e.getMessage().contains("already exists")){
//
//            }else {
//                e.printStackTrace();
//            }
//        }
//    }
//    public void insertBaseBusinessInfo(BaseBusinessInfo businessInfo,String job_id){
//
//        // 插入公司名称
//        String query = String.format("merge (n:Company{qichacha_id:'%s',name:'%s'}) ",
//                businessInfo.getCompany_unique(),
//                businessInfo.getCompany_name()
//                );
//        run(query);
//        // 插入法定代表人或合伙制代表人
//        if(businessInfo.getLegal_representative_unique() == null){
//            return;
//        }else if(businessInfo.getLegal_representative_unique().startsWith("pl_")){
//            //法定代表人
//            String q1 = String.format("merge (n:People{qichacha_id:'%s',name:'%s'}) ",
//                    businessInfo.getLegal_representative_unique(),
//                    businessInfo.getLegal_representative()
//            );
//            run(q1);
//            String r1 = String.format("match (u),(v) where u.qichacha_id = '%s'" +
//                            "and v.qichacha_id = '%s'" +
//                            "merge (u)-[:LegalRepresentative{legalRepresentativeType:'法定代表人'}]->(v) ",
//                    businessInfo.getLegal_representative_unique(),
//                    businessInfo.getCompany_unique()
//            );
//            run(r1);
//
//        }else {
//            //合伙制代表人
//            String q2= String.format("merge (n:Company{qichacha_id:'%s',name:'%s'}) ",
//                    businessInfo.getLegal_representative_unique(),
//                    businessInfo.getLegal_representative()
//            );
//            run(q2);
//            String r2 = String.format("match (u),(v) where u.qichacha_id = '%s'" +
//                            "and v.qichacha_id = '%s'" +
//                            "merge (u)-[:LegalRepresentative{legalRepresentativeType:'执行事务合伙人'}]->(v) ",
//                    businessInfo.getLegal_representative_unique(),
//                    businessInfo.getCompany_unique()
//            );
//            run(r2);
//
//        }
//
//    }
//    public void insertEquityPledge(List<EquityPledge> equityPledges,String job_id){
//        for(EquityPledge equityPledge:equityPledges){
//            // 建立质权人节点
//            if(equityPledge.getPledgee_unique() == null){
//                return;
//            }else if(equityPledge.getPledgee_unique().startsWith("pl_")){
//                //个人质权人
//                String q1 = String.format("merge (n:People{qichacha_id:'%s',name:'%s'}) ",
//                        equityPledge.getPledgee_unique(),
//                        equityPledge.getPledgee_name()
//                );
//                run(q1);
//            }else {
//                //企业质权人
//                String q2= String.format("merge (n:Company{qichacha_id:'%s',name:'%s'}) ",
//                        equityPledge.getPledgee_unique(),
//                        equityPledge.getPledgee_name()
//                );
//                run(q2);
//            }
//            // 建立出质人节点
//            if(equityPledge.getPledgor_unique() == null){
//                return;
//            }else if(equityPledge.getPledgor_unique().startsWith("pl_")){
//                //个人质权人
//                String q1 = String.format("merge (n:People{qichacha_id:'%s',name:'%s'}) ",
//                        equityPledge.getPledgor_unique(),
//                        equityPledge.getPledgor_name()
//                );
//                run(q1);
//                //建立关系
//                String r1 = String.format("match (u),(v) where u.qichacha_id = '%s'" +
//                                "and v.qichacha_id = '%s'" +
//                                "merge (u)-[:Pledge{PledgeType:'个人股权出质',StockNumber:'%s',Status:'%s'}]->(v)",
//                        equityPledge.getPledgor_unique(),
//                        equityPledge.getPledgee_unique(),
//                        equityPledge.getStock_number(),
//                        equityPledge.getStatus()
//                );
////                run(r1);
//            }else {
//                //企业质权人
//                String q2= String.format("merge (n:Company{qichacha_id:'%s',name:'%s'}) ",
//                        equityPledge.getPledgor_unique(),
//                        equityPledge.getPledgor_name()
//                );
//                run(q2);
//                //建立关系
//                String r2 = String.format("match (u),(v) where u.qichacha_id = '%s'" +
//                                "and v.qichacha_id = '%s'" +
//                                "merge (u)-[:Pledge{PledgeType:'公司股权出质',StockNumber:'%s',Status:'%s'}]->(v)",
//                        equityPledge.getPledgor_unique(),
//                        equityPledge.getPledgee_unique(),
//                        equityPledge.getStock_number(),
//                        equityPledge.getStatus()
//                );
//                run(r2);
//            }
//
//
//        }
//    }
//    public void insertBaseShareholderInfo(List<BaseShareholderInfo> baseShareholderInfos, String job_id){
//        for(BaseShareholderInfo baseShareholderInfo:baseShareholderInfos){
//            if(baseShareholderInfo.getShareholder_unique() == null){
//                return;
//            }else if(baseShareholderInfo.getShareholder_unique().startsWith("pl_")){
//                //个人股东
//                String q1 = String.format("merge (n:People{qichacha_id:'%s',name:'%s'}) ",
//                        baseShareholderInfo.getShareholder_unique(),
//                        baseShareholderInfo.getShareholder()
//                );
//                run(q1);
//                String r1 = String.format("match (u),(v) where u.qichacha_id = '%s'" +
//                                "and v.qichacha_id = '%s'" +
//                                "merge (u)-[:Shareholder{ShareholderType:'个人持股',ShareholdingRatio:'%s',SubscriptionAmount:'%s',ChangeDate:'-'}]->(v)",
//                        baseShareholderInfo.getCompany_unique(),
//                        baseShareholderInfo.getShareholder_unique(),
//                        baseShareholderInfo.getShareholding_ratio(),
//                        baseShareholderInfo.getSubscription_amount()
//                );
//                run(r1);
//
//            }else {
//                //企业股东
//                String q2= String.format("merge (n:Company{qichacha_id:'%s',name:'%s'}) ",
//                        baseShareholderInfo.getShareholder_unique(),
//                        baseShareholderInfo.getShareholder()
//                );
//                run(q2);
//                String r2 = String.format("match (u),(v) where u.qichacha_id = '%s'" +
//                                "and v.qichacha_id = '%s'" +
//                                "merge (u)-[:Shareholder{ShareholderType:'公司持股',ShareholdingRatio:'%s',SubscriptionAmount:'%s',ChangeDate:'-'}]->(v)",
//                        baseShareholderInfo.getCompany_unique(),
//                        baseShareholderInfo.getShareholder_unique(),
//                        baseShareholderInfo.getShareholding_ratio(),
//                        baseShareholderInfo.getSubscription_amount());
//                run(r2);
//
//            }
//
//        }
//    }
//    public void insertBaseChange(List<BaseChange> baseChanges,String job_id){
//        for (BaseChange baseChange:baseChanges){
//            if(baseChange.getBefore_text_link() != null
//                    && baseChange.getBefore_text_link().length()>0
//                    && baseChange.getBefore_text().contains("退出")){
//                if(baseChange.getBefore_text_link().startsWith("pl_"))
//                {
//                    //个人投资人退出
//                    String text = baseChange.getChange_project();
//                    if (text.contains("投资人")||text.contains("股东")){
//                        logger.debug(baseChange.getBefore_text());
//                        String q1= String.format("merge (n:People{qichacha_id:'%s',name:'%s'}) ",
//                                baseChange.getBefore_text_link(),
//                                baseChange.getBefore_text_em()
//                        );
//                        run(q1);
//                    }
//                }else {
//                    //企业退出
//                    String q1= String.format("merge (n:Company{qichacha_id:'%s',name:'%s'}) ",
//                            baseChange.getBefore_text_link(),
//                            baseChange.getBefore_text_em()
//                    );
//                    run(q1);
//                }
//                String r2 = String.format("match (u),(v) where u.qichacha_id = '%s'" +
//                                "and v.qichacha_id = '%s'" +
//                                "merge (u)-[:Shareholder{ShareholderType:'前股东',ShareholdingRatio:'-',SubscriptionAmount:'-',ChangeDate:'%s'}]->(v)",
//                        baseChange.getCompany_unique(),
//                        baseChange.getBefore_text_link(),
//                        baseChange.getChange_date());
//                run(r2);
//
//
//            }
//        }
//    }
//    public void insertBaseRegisterInfo(BaseRegisterInfo baseRegisterInfo,String job_id){
//        // 插入公司名称
//        String query = String.format("merge (n:Company{qichacha_id:'%s',name:'%s'}) ",
//                baseRegisterInfo.getCompany_unique(),
//                baseRegisterInfo.getCompany_full_name()
//        );
//        run(query);
//    }
//    }